const app = getApp()
Page({
  data: {
    isSubmit: 1,
    content:'',
    selId: []
  },
  onLoad: function (options) {
      var that = this
      this.vote_index(this)
  },
  vote_index:function(e){
	  app.request(app.apiDomain+'index.php?c=vote&a=index',{},res=>{
	      console.log(res);
	      // 判断今日是否提交过
	    if(res.data.data.user.id){
	        // 提交过
	        e.setData({
	            list: res.data.data.list,
	            total: res.data.data.ticket,
	            user: res.data.data.user,
	            isSubmit: 0,
	            content: res.data.data.user.content,
	            selId: res.data.data.user.vote_id
	        })
	    }else{
	        e.setData({
	            list: res.data.data.list,
	            total: res.data.data.ticket,
	        })
	    }
	  })
  },
  checkboxChange(e){
    console.log(e);
    console.log(e.detail.value.join(','));
    this.setData({
        selId: e.detail.value.join(',')
    })
  },
  inputText(e){
    console.log(e);
    this.setData({
        content: e.detail.value
    })
  },
  submit(){
    console.log(22);
    if(this.data.isSubmit==0){ return false }
    var that = this
    app.request(app.apiDomain+'index.php?c=vote&a=apply',{
        vote_ids: that.data.selId,
        content: that.data.content
    },res=>{
        console.log(res);
        if(res.data.status==1){
            // 投票成功
            // that.setData({
            //     isSubmit: 0
            // })
			that.vote_index(that)
            wx.showToast({
                title: '提交成功,感谢您的建议',
                icon: 'none',
                duration: 2000
            })
//             setTimeout(()=>{
//                 // 刷新本页面
//                 that.onLoad()
//             },2000)
        }else{
            wx.showToast({
                title: res.data.info,
                icon: 'none',
                duration: 2000
            })
        }
    })
  }
})